import Vue from 'vue'
import VueRouter from 'vue-router'
import Router from 'vue-router'


Vue.use(VueRouter)
Vue.use(Router)



const routes = [
  {
    path: '/',
    name: 'navbar.home',
    component: () => import('../views/HomeView.vue'),
    meta: { 
      transition: 'slide-left',
      i18n:'navbar.home'
     },
    // redirect: { name: 'login.login' }
  },
  {
    path: "/login",
    name: "login.login",
    component: () => import('../views/LoginView.vue'),
    meta: { 
      transition: 'slide-left',
      i18n:'login.login' 
    },
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// token登录判断
const isLogin = sessionStorage.getItem("ice-token")
router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && !isLogin) {
    next({path:'/login'}) // 阻止路由跳转
  }else if(isLogin&&to.path == '/login'){
    next({path:'/'})
  } else {
    next() // 正常运行
  }
})

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
